﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc;
using MalkiSumWeb.Models;

namespace MalkiSumWeb.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/
        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
        }

        SumrizDBEntities dbEntities = new SumrizDBEntities();
        public ActionResult Index()
        {
            return View();
        }

        #region ActionType
        public ActionResult ActionType()
        {
            return View();
        }
        [GridAction]
        public ActionResult SelectActionType()
        {
            return View(new GridModel(dbEntities.ActionTypes));
        }
        [GridAction]
        public ActionResult InsertActionType(string NameAr, string NameEn, string IconUrl)
        {
            ActionType aType = new ActionType();

            aType.NameAr = NameAr;
            aType.NameEn = NameEn;
            aType.IconUrl = IconUrl;
            dbEntities.ActionTypes.AddObject(aType);
            dbEntities.SaveChanges();

            return View(new GridModel(dbEntities.ActionTypes));
        }
        [GridAction]
        public ActionResult UpdateActionType(int ID, string NameAr, string NameEn, string IconUrl)
        {
            ActionType aType = dbEntities.ActionTypes.Where(a => a.ID == ID).FirstOrDefault();
            if (aType != null)
            {
                aType.NameAr = NameAr;
                aType.NameEn = NameEn;
                aType.IconUrl = IconUrl;
                dbEntities.SaveChanges();
            }
            return View(new GridModel(dbEntities.ActionTypes));
        }
        [GridAction]
        public ActionResult DeleteActionType(int ID)
        {
            ActionType aType = dbEntities.ActionTypes.Where(a => a.ID == ID).FirstOrDefault();
            if (aType != null)
            {
                dbEntities.ActionTypes.DeleteObject(aType);
                dbEntities.SaveChanges();
            }
            return View(new GridModel(dbEntities.ActionTypes));
        }
        #endregion

        #region Field
        public ActionResult Field()
        {
            var list = dbEntities.Fields.ToList();

            Field field = new Models.Field ();
            
            field.NameEn = "Select Field";
            list.Insert(0, field);
            ViewData["Fields"] =list; 
            return View();
        }
        [GridAction]
        public ActionResult SelectField()
        {
            return View(new GridModel(dbEntities.Fields.Select(e => new
                 {
                     e.ID,
                     e.NameAr,
                     e.NameEn,
                     e.FieldID,
                     e.KeyWords
                 })));
        }
        [GridAction]
        public ActionResult InsertField(string NameAr, string NameEn, string KeyWords, int FieldID )
        {
            Field field = new Field();

            field.NameAr = NameAr;
            field.NameEn = NameEn;
            field.KeyWords = KeyWords;
            if (FieldID>0)
            {
                field.FieldID = FieldID;
            }
            dbEntities.Fields.AddObject(field);
            dbEntities.SaveChanges();

            return View(new GridModel(dbEntities.Fields.Select(e => new
            {
                e.ID,
                e.NameAr,
                e.NameEn,
                e.FieldID,
                e.KeyWords
            })));
        }
        [GridAction]
        public ActionResult UpdateField(int ID, string NameAr, string NameEn, string KeyWords, int FieldID)
        {
            Field field = dbEntities.Fields.Where(a => a.ID == ID).FirstOrDefault();
            if (field != null)
            {
                field.NameAr = NameAr;
                field.NameEn = NameEn;
                field.KeyWords = KeyWords;
                if (FieldID>0)
                {
                    field.FieldID = FieldID;
                }
                else
                {
                    field.FieldID = null;
                }
                dbEntities.SaveChanges();
            }
            return View(new GridModel(dbEntities.Fields.Select(e => new
            {
                e.ID,
                e.NameAr,
                e.NameEn,
                e.FieldID,
                e.KeyWords
            })));
        }
        [GridAction]
        public ActionResult DeleteField(int ID)
        {
            Field field = dbEntities.Fields.Where(a => a.ID == ID).FirstOrDefault();
            if (field != null)
            {
                dbEntities.Fields.DeleteObject(field);
                dbEntities.SaveChanges();
            }
            return View(new GridModel(dbEntities.Fields.Select(e => new
            {
                e.ID,
                e.NameAr,
                e.NameEn,
                e.FieldID,
                e.KeyWords
            })));
        }
        #endregion
   
        #region TimeZone
        
        public ActionResult TimeZone()
        {
            return View();
        }
        [GridAction]
        public ActionResult SelectTimeZone()
        {
            return View(new GridModel(dbEntities.TimeZones));
        }
        [GridAction]
        public ActionResult InsertTimeZone(string NameAr, string NameEn, int Value)
        {
            Models.TimeZone timeZone = new Models.TimeZone();

            timeZone.NameAr = NameAr;
            timeZone.NameEn = NameEn;
            timeZone.Value = Value;
            dbEntities.TimeZones.AddObject(timeZone);
            dbEntities.SaveChanges();

            return View(new GridModel(dbEntities.TimeZones));
        }
        [GridAction]
        public ActionResult UpdateTimeZone(int ID, string NameAr, string NameEn, int Value)
        {
           Models.TimeZone timeZone = dbEntities.TimeZones.Where(a => a.ID == ID).FirstOrDefault();
            if (timeZone != null)
            {
                timeZone.NameAr = NameAr;
                timeZone.NameEn = NameEn;
                timeZone.Value = Value;
                dbEntities.SaveChanges();
            }
            return View(new GridModel(dbEntities.TimeZones));
        }
        [GridAction]
        public ActionResult DeleteTimeZone(int ID)
        {
            Models.TimeZone timeZone = dbEntities.TimeZones.Where(a => a.ID == ID).FirstOrDefault();
            if (timeZone != null)
            {
                dbEntities.TimeZones.DeleteObject(timeZone);
                dbEntities.SaveChanges();
            }
            return View(new GridModel(dbEntities.TimeZones));
        }
        #endregion

        #region Language
        public ActionResult Language()
        {
            return View();
        }
        [GridAction]
        public ActionResult SelectLanguage()
        {
            return View(new GridModel(dbEntities.Languages));
        }
        [GridAction]
        public ActionResult InsertLanguage(string NameAr, string NameEn)
        {
            Language language = new Language();

            language.NameAr = NameAr;
            language.NameEn = NameEn;
            dbEntities.Languages.AddObject(language);
            dbEntities.SaveChanges();

            return View(new GridModel(dbEntities.Languages));
        }
        [GridAction]
        public ActionResult UpdateLanguage(int ID, string NameAr, string NameEn)
        {
            Language language = dbEntities.Languages.Where(a => a.ID == ID).FirstOrDefault();
            if (language != null)
            {
                language.NameAr = NameAr;
                language.NameEn = NameEn;
                dbEntities.SaveChanges();
            }
            return View(new GridModel(dbEntities.Languages));
        }
        [GridAction]
        public ActionResult DeleteLanguage(int ID)
        {
            Language language = dbEntities.Languages.Where(a => a.ID == ID).FirstOrDefault();
            if (language != null)
            {
                dbEntities.Languages.DeleteObject(language);
                dbEntities.SaveChanges();
            }
            return View(new GridModel(dbEntities.Languages));
        }
        #endregion

        #region Country
        public ActionResult Country()
        {
            return View();
        }
        [GridAction]
        public ActionResult SelectCountry()
        {
            return View(new GridModel(dbEntities.Countries));
        }
        [GridAction]
        public ActionResult InsertCountry(string NameAr, string NameEn)
        {
            Country country = new Country();

            country.NameAr = NameAr;
            country.NameEn = NameEn;
            dbEntities.Countries.AddObject(country);
            dbEntities.SaveChanges();

            return View(new GridModel(dbEntities.Countries));
        }
        [GridAction]
        public ActionResult UpdateCountry(int ID, string NameAr, string NameEn)
        {
            Country country = dbEntities.Countries.Where(a => a.ID == ID).FirstOrDefault();
            if (country != null)
            {
                country.NameAr = NameAr;
                country.NameEn = NameEn;
                dbEntities.SaveChanges();
            }
            return View(new GridModel(dbEntities.Countries));
        }
        [GridAction]
        public ActionResult DeleteCountry(int ID)
        {
            Country country = dbEntities.Countries.Where(a => a.ID == ID).FirstOrDefault();
            if (country != null)
            {
                dbEntities.Countries.DeleteObject(country);
                dbEntities.SaveChanges();
            }
            return View(new GridModel(dbEntities.Countries));
        }
        #endregion
    }
}
